<?php
/**
 * 商家注册控制
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-04-22 18:59:59
 */
namespace app\bis\controller;

use think\Controller;
use think\Request;

class Register extends Controller
{
    // 显示商家注册
    public function index()
    {
        // 获取一级城市
        $citys = model('city')->getNormalFirstCity();
        // 获取一级栏目
        $categorys = model('category')->getNormalFirstCategory();
        // $citys = [];
        // $categorys = [];
        return $this->fetch('',[
            'citys' => $citys,
            'categorys' => $categorys,
        ]);
    }

    // 注册商家
    public function add(Request $request)
    {
        if (!$request->isPost()) {
            $this->error('Request Data Error!');
        }

        // 获取表单值
        $data = $request->post();
        // dump($data);exit;

        // 校验表单数据
        $vail = $this->validate($data,'register.add');
        if (TRUE !== $vail) {
            $this->error($vail);
        }

        // 获取经纬度
        // $LngLat= \Baidumap::getLngLat($data['address']);
        /*if (empty($LngLat)||0!=$LngLat['status']||1!=$LngLat['result']['precise']) {
            $this->error('无法获取地址数据，或者匹配地址不精准！');
        }*/

        // 商户基本信息入库
        $city_path = empty($data['se_city_id']) ? $data['city_id'] : $data['city_id'].','.$data['se_city_id'];
        $description = empty($data['description'])?'':$data['description'];
        $bisData = [
            'name'         => $data['name'],
            'city_id'      => $data['city_id'],
            'city_path'    => $city_path,
            'logo'         => $data['logo'],
            'licence_logo' => $data['licence_logo'],
            'description'  => $description,
            'bank_name'    => $data['bank_name'],
            'bank_info'    => $data['bank_info'],
            'bank_user'    => $data['bank_user'],
            'faren'        => $data['faren'],
            'faren_tel'    => $data['faren_tel'],
            'email'        => $data['email'],
        ];
        $bisId = model('Bis')->add($bisData);
        if (!$bisId) {
            $this->error('添加商户信息失败！');
        }

        // 总店相关信息入库
        $data['category'] = '';
        if (!empty($data['se_category_id'])) {
            $data['category'] = implode('|',$data['se_category_id']);
        }
        $xpoint = empty($LngLat['result']['location']['lng'])?'':$LngLat['result']['location']['lng'];
        $ypoint = empty($LngLat['result']['location']['lat'])?'':$LngLat['result']['location']['lat'];
        $locatData = [
            'bis_id'        => $bisId,
            'name'          => $data['name'],
            'tel'           => $data['tel'],
            'contact'       => $data['contact'],
            'logo'          => $data['logo'],
            'address'       => $data['address'],
            'xpoint'        => $xpoint,
            'ypoint'        => $ypoint,
            'open_time'     => $data['open_time'],
            'content'       => empty($data['content'])?'':$data['content'],
            'is_main'       => 1, // 默认为总店
            'api_address'   => $data['address'],
            'city_id'       => $data['city_id'],
            'city_path'     => $city_path,
            'category_id'   => $data['category_id'],
            'category_path' => $data['category_id'].','.$data['category'],
        ];
        $bislocatId = model('BisLocation')->add($locatData);
        if (!$bislocatId) {
            $this->error('添加总店失败！');
        }

        // 商户个人信息
        $code = mt_rand(100, 10000);
        $accountData = [
            'username'        => $data['username'],
            'bis_id'          => $bisId,
            'code'            => $code,
            'password'        => md5($data['password'].$code),
            'email'           => $data['email'],
            'is_main'         => 1, // 默认为总管理员
        ];
        $accountId = model('BisAccount')->add($accountData);

        if (!$accountId) {
            $this->error('添加商户个人信息失败！');
        }

        // 发送邮件给用户
        /*$url = url('/bis/register/waiting', ['id'=>$bisId], false, 'true');
        $title = 'o2o入驻申请通知';
        $content = '您已提交的入驻申请需要等待平台方审核；您可以通过点击链接<a href="'.$url.' target="_blank">查看连接</a>查看审核状态！';
        if (!$this->sendEmail($data['email'], $title, $content)) {
            $this->error('申请失败！');
        }*/
        // 释放内存
        $bisData = [];
        $locatData = [];
        $accountData = [];
        $this->success('申请成功！',url('register/waiting', ['id'=>$bisId]));
    }

    private function sendEmail($email='', $title='', $content='')
    {
       return \phpmailer\Email::send($email='', $title='', $content='');
    }

    public function waiting($id=0)
    {
        if (0>=intval($id)) {
            $this->error('Request Data Error!');
        }
        $detail = model('bis')->field('status')->where('id',$id)->find();
        // dump($detail);
        $this->assign('detail',$detail);
        return $this->fetch();
    }

}
